package zmee.pmas.op;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author cb
 * @DateTime 2022/12/5 15:58
 * @Description 收费、操作插件
 */
public class PaymanageOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("billno");
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        String operationKey = e.getOperationKey();
        DynamicObject[] dataEntities = e.getDataEntities();
        if ("audit".equals(operationKey)) {
            for (DynamicObject dataEntity : dataEntities) {
                // 审核之后给生成的 开标准备 中的 评标设置单据体赋值
                List<String> typsList = new ArrayList<>();
                // 单据编号
                String billno = dataEntity.getString("billno");
                QFilter qFilterBillno = new QFilter("billno", "=", billno);
                // 查询 标书编制
                DynamicObject loadSingle = BusinessDataServiceHelper.loadSingle("src_biddoc", "id", new QFilter[]{qFilterBillno});
                if (null != loadSingle) {
                    Object pkValue = loadSingle.getPkValue();
                    QFilter qFilterParentid = new QFilter("parentid", "=", pkValue);
                    // 查询 采购方招标文件
                    DynamicObject biddocSrc = BusinessDataServiceHelper.loadSingle("src_biddoc_src",
                            "id,entryentity,entryentity.packfiletype", new QFilter[]{qFilterParentid});
                    if (null != biddocSrc) {
                        DynamicObjectCollection entryentity = biddocSrc.getDynamicObjectCollection("entryentity");
                        for (DynamicObject entry : entryentity) {
                            String packfiletype = entry.getString("packfiletype");
                            typsList.add(packfiletype);
                        }
                    }
                }
                // 查询中间表放入Map中
                Map<String, Long> bslxzblxMap = new HashMap<>();
                QFilter qFilter = new QFilter("billno", "=", "zmee_001");
                DynamicObjectCollection querys = QueryServiceHelper.query("zmee_bslxzblx",
                        "id,entryentity,entryentity.zmee_combofield,entryentity.zmee_basedatafield", new QFilter[]{qFilter});
                for (DynamicObject query : querys) {
                    bslxzblxMap.put(query.getString("entryentity.zmee_combofield"), query.getLong("entryentity.zmee_basedatafield"));
                }
                // 查询 开标准备
                DynamicObject bidopen = BusinessDataServiceHelper.loadSingle("src_bidopen",
                        "id", new QFilter[]{qFilterBillno});
                if (null != bidopen) {
                    QFilter qFilterParentid = new QFilter("parentid", "=", bidopen.getPkValue());
                    DynamicObject dynamicObject = QueryServiceHelper.queryOne("src_bidopen_config", "id", new QFilter[]{qFilterParentid});
                    if (null != dynamicObject) {
                        DynamicObject bidopenConfig = BusinessDataServiceHelper.loadSingle(dynamicObject.get("id"), "src_bidopen_config");
                        if (null != bidopenConfig) {
                            System.out.println("bidopenConfig.getPkValue() = " + bidopenConfig.getPkValue());
                            DynamicObjectCollection entryentitys = bidopenConfig.getDynamicObjectCollection("entryentity");
                            entryentitys.clear();
                            for (int i = 0; i < typsList.size(); i++) {
                                Long indextypeId = bslxzblxMap.get(typsList.get(i));
                                if (null != indextypeId) {
                                    DynamicObject addNew = entryentitys.addNew();
                                    addNew.set("indextype", indextypeId);
                                    addNew.set("seq", i + 1);
                                    addNew.set("entrystatus", "A");
                                }
                            }
                            SaveServiceHelper.save(new DynamicObject[]{bidopenConfig});
                        }
                    }
                }
            }
        }
    }
}